package cn.tedu.shopol.taojin.controller;

import cn.tedu.shopol.taojin.ex.InsertException;
import cn.tedu.shopol.taojin.ex.InvalidParameterException;
import cn.tedu.shopol.taojin.ex.PhoneDuplicateException;
import cn.tedu.shopol.taojin.ex.ServiceException;
import cn.tedu.shopol.taojin.util.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ServiceException.class)
    public R handlerException(Exception e){
        if (e instanceof PhoneDuplicateException){
            return R.failure(R.State.ERR_PHONE_DUPLICATE,e);
        }else if (e instanceof InsertException){
            return R.failure(R.State.ERR_INSERT,e);
        }else if (e instanceof InvalidParameterException){
            return R.failure(R.State.ERR_INVALID_PARAMETER,e);
        }
        return null;
    }
}
